<?php
declare (strict_types=1);

namespace app\common\model;

use think\facade\Cache;
use think\Model;

//系统菜单数据表
class AdminMenuModel extends Model
{
    protected $table = 'utf8mb4_admin_menu';
    protected $cache_time = 0; //数据缓存时间0表示永久缓存
    public $error = '';
    public $icon = '&#xe696;'; //默认菜单图标
    public $rule = [
        'name' => 'require|max:10',
        'sort' => 'require|number',
        'pid' => 'require|number',
        'is_menu' => 'require|in:0,1',
    ];
    public $message = [
        'name.require' => '名称不能为空',
        'name.max' => '名称最长10个字符',
        'sort.require' => '排序号不能为空',
        'sort.number' => '排序号只能是数字',
        'pid.require' => '父菜单不能为空',
        'pid.number' => '父菜单只能是数字',
        'is_menu.require' => '是否是菜单,必选',
        'is_menu.in' => '是否是菜单选择错误',
    ];

    public $type = [
        1 => '|-顶部菜单',
        2 => '|-----二级菜单',
        3 => '|---------三级菜单'
    ];

    //递归获取全部菜单
    public function getMenu(int $pid = 0)
    {
        $key = config('app.menu_hash_table');
        $cache_key = $key . (string)$pid;
        $list = Cache::store('redis')->get($cache_key);
        if (empty($list)) {
            $list = $this::where([['pid', '=', $pid], ['delete_time', '=', 0]])->order('sort asc')->select()->toArray();
            if (empty($list)) return [];
            foreach ($list as $key => $value) {
                Cache::store('redis')->set(config('app.menu_id_info') . $value['id'], $value, $this->cache_time);
                Cache::store('redis')->set(config('app.menu_url_info') . $value['url'], $value, $this->cache_time);
                $list[$key]['children'] = $this->getMenu($value['id']);
                if (empty($list[$key]['children'])) {
                    unset($list[$key]['children']);
                }
            }
        }
        Cache::store('redis')->set($cache_key, $list, $this->cache_time);
        return $list;
    }

    //获取按照lev_str菜单
    public function getMenuLev()
    {
        $cache_key = config('app.all_menu_key');
        $list = Cache::store('redis')->get($cache_key);
        if (!$list) {
            $field = '*';
            $list = [];
            $data = $this->field($field)->where([['delete_time', '=', 0], ['pid', '=', 0]])->order('sort asc')->select()->toArray();
            foreach ($data as $key => $item) {
                $item['mark'] = '|-';
                $list[] = $item;
                $list1 = $this->field($field)->where([['delete_time', '=', 0], ['pid', '=', $item['id']]])->order('sort asc')->select()->toArray();
                foreach ($list1 as $k => $v) {
                    $v['mark'] = '|------';
                    $list[] = $v;
                    $list2 = $this->field($field)->where([['delete_time', '=', 0], ['pid', '=', $v['id']]])->order('sort asc')->select()->toArray();
                    foreach ($list2 as $vv) {
                        $vv['mark'] = '|------------';
                        $list[] = $vv;
                    }
                }
            }
            Cache::store('redis')->set($cache_key, $list, $this->cache_time);
        }
        return $list;
    }
}